package com.baizhi.controller;


import com.baizhi.entity.Dept;
import com.baizhi.entity.Emp;
import com.baizhi.service.EmpService;

import com.baizhi.vo.CollectionVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

@RestController
@RequestMapping("emp")
public class EmpController {

    @Autowired
    private EmpService empService;

    //分页查询
    @RequestMapping("ShowAllEmp")
    public String showAllEmp(@RequestParam(value = "pageNo",defaultValue = "1") int pageNo, HttpServletRequest request){

        if(pageNo == 0)pageNo=1;
        List<Emp> list = empService.queryPageEmp(pageNo);
        int totalPage = empService.queryAllTotal();
        request.setAttribute("list",list);
        request.setAttribute("totalPage",totalPage);
        return "view/emplist";
    }

    @RequestMapping("ajax")
    public @ResponseBody List<Emp> ajax(){
        List<Emp> list = empService.queryPageEmp(1);
        System.out.println(list);
        return list;
    }



    //增加
    @RequestMapping("addEmp")
    public String addEmp(Emp emp,Integer id){
        empService.addEmp(emp,id);
        return "redirect:/emp/ShowAllEmp";
    }



    //查所有部门
    @RequestMapping("showAllDept")
    @ResponseBody
    public List<Dept> showAllDept(HttpServletResponse response) throws IOException {
        List<Dept>list = empService.queryAllDept();
//        response.setContentType("application/json;charset=UTF-8");
//        PrintWriter out = response.getWriter();
//
//        String json = JSON.toJSONString(list);
//        out.print(json);
//        out.flush();
        return list;
    }

    //删除
    @RequestMapping("delEmp")
    public String delEmp(Integer id){
        empService.delEmpbyId(id);
        return "redirect:/emp/ShowAllEmp";
    }


    //批量删除
    @RequestMapping("delEmps")
    public String del(CollectionVO collectionVO){
        empService.delEmp(collectionVO.getList());
        return "redirect:/emp/ShowAllEmp";
    }

    //单个查询
    @RequestMapping("queryEmp")
    public String queryEmp(Integer id,HttpServletRequest request){
        Emp emp = empService.queryEmp(id);
        HttpSession session = request.getSession();
        session.setAttribute("uemp" , emp);
        return "redirect:/view/updateEmpl.jsp";
    }

    //修改
    @RequestMapping("update")
    public String update(Emp emp1,Integer id2,HttpServletRequest request){
        Object object = request.getSession().getAttribute("uemp");
        Emp emp = (Emp)object;
        emp1.setId(emp.getId());
        empService.updEmp(emp1,id2);
        return "redirect:/emp/ShowAllEmp";
    }




}
